package www.houdoudou.com.customer;

import com.houdoudou.com.machine.MachineDistributeManagement;
import com.houdoudou.com.machine.MachineManage;
import com.houdoudou.com.owner.OwnerManagement;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import www.houdoudou.com.supplier.IndexManagement;
import www.houdoudou.com.supplier.SupplierManagement;

public class PullSuppliers {

	  public static JSONObject  pull(String jsonData){
   	   
    	  JSONObject result = new JSONObject();
    	  JSONArray Suppliers= new JSONArray();
    		JSONObject jsonfrom= JSONObject.fromObject(jsonData);				
    	
    		int Machine_ID=jsonfrom.getInt("Machine_ID");
    		 //  PreparedStatement pstm=null; 
    		  // Statement  stm=null;
    		  // ResultSet rs=null; 	
    		  // String CurrentConnUser=CommonMathFunction.genRandomNum(10);
    		  // Connection conn=ConnFactory.getCurrentConnection(CurrentConnUser);
    		  /*---------查询机器-----------*/
    		   JSONObject jsonInner1=jsonfrom;
    		   jsonInner1.put("FunctionCode", 1);
    		   jsonInner1.put("Machine_ID", Machine_ID);
    		   JSONObject jsonInner1B=MachineManage.manageMachine(jsonInner1.toString());
    		   String Owner_Telnum=jsonInner1B.getString("Owner_Telnum");
    		   String Machine_Time_M =jsonInner1B.getString("Machine_Time_M");
    		   String Machine_Time_N=jsonInner1B.getString("Machine_Time_N");
    		   String Machine_Time_E=jsonInner1B.getString("Machine_Time_E");
    		   String Machine_Temp_Config=jsonInner1B.getString("Machine_Temp_Config");
    		   String Machine_Time_Config=jsonInner1B.getString("Machine_Time_Config");
    		   String Machine_Address=jsonInner1B.getString("Machine_Address");
    		   String Machine_Name=jsonInner1B.getString("Machine_Name"); 
    		   
    		  /*---------业主是否授权-----------*/
    		  JSONObject jsonInner2=jsonfrom;
   		        jsonInner2.put("FunctionCode", 1);
   		        jsonInner2.put("Owner_Telnum",Owner_Telnum);
   		        JSONObject jsonInner2B=OwnerManagement.managerOwner(jsonInner2.toString());
   		        String Ownner_isAuthority=jsonInner2B.getString("Ownner_isAuthority");
   		       /*---------业主 已 授权-----------*/
   		        if(Ownner_isAuthority.equals("1")){
   		        	//分早中晚查询供应商。
   		        
   		         for(int i=1;i<4;i++){
   		        	 if(Machine_Time_Config.charAt(i-1)=='1'){
   		        		 String Time_Flag= String.valueOf(i);
   		        		 /*---------查询本时间段的所有供应商-----------*/
   		        	  JSONObject jsonInner3=jsonfrom;
   	   		        jsonInner3.put("FunctionCode", 5);
   	   		        jsonInner3.put("Query_Way",5);
   	   		        jsonInner3.put("Time_Flag",Time_Flag);
   	   		        JSONObject jsonInner3B=MachineDistributeManagement.manage(jsonInner3.toString());
   	   		             if(jsonInner3B.getBoolean("ActionStatus")){
   	   		            	 JSONArray Distributes= jsonInner3B.getJSONArray("Distributes");
   	   		            	   for(int j=0;j<Distributes.size();j++){
   	   		            		String Supplier_Telnum= Distributes.getJSONObject(j).getString("Supplier_Telnum");
   	   		            	          /*---------查询该供应商信息-----------*/ 
   	   		            	                JSONObject jsonInner4=jsonfrom;
   	     	   		                        jsonInner4.put("FunctionCode", 1);
   	     	   		                         jsonInner4.put("Supplier_Telnum",Supplier_Telnum);	                        
   	     	   		                         JSONObject jsonInner4B = SupplierManagement.managerSupplier(jsonInner4.toString());
   	     	   		                         if(jsonInner4B.getBoolean("ActionStatus")&&jsonInner4B.getString("Supplier_isAuthority").equals("1")){
   	     	   		                        	 
   	     	   		                      /*---------查询该供应商有没有   Timepflag tempflag都对的目录，没有就不会把改供应商返回。-----------*/ 
   	     	   		                      JSONObject jsonInner5=jsonfrom;
   	     	   		                         jsonInner5.put("FunctionCode", 5);
   	     	   				                 jsonInner5.put("Query_Way", 2); 
 	     	   		                         jsonInner5.put("Supplier_Telnum",Supplier_Telnum);	 
 	     	   		                         jsonInner5.put("Index_Time_Flag",Time_Flag);	 
 	     	   		                         jsonInner5.put("Index_Temp_Flag",String.valueOf(Machine_Temp_Config.charAt(i-1)));	
 	     	   		                           
 	     	   		                        JSONObject jsonInner5B=IndexManagement.managerIndex(jsonInner5.toString());
 	     	   		                        boolean checkSupplierHasIndex=jsonInner5B.getBoolean("ActionStatus")&&(jsonInner5B.getJSONArray("Indexes").size()>0);
 	     	   		                            if(checkSupplierHasIndex){
 	     	   		                            	
   	     	   		                        	JSONObject supplier= new JSONObject();
   	     	   		                    
   	     	   		                    supplier.put("Supplier_Name", jsonInner4B.getString("Supplier_Name"));
    	     	   					    supplier.put("Service_Telnum",jsonInner4B.getString("Service_Telnum"));  
   	     	   						    supplier.put("Major_Business", jsonInner4B.getString("Major_Business"));					  
   	     	   						    supplier.put("Product_Total_Num", jsonInner4B.getInt("Product_Total_Num"));
   	     	   						    supplier.put("Minute_NoOrder", jsonInner4B.getInt("Minute_NoOrder"));
   	     	   						    supplier.put("Sale_Total",jsonInner4B.getInt("Sale_Total"));
   	     	   		                    if(i==1) supplier.put("Arrive_Time",Machine_Time_M);
   	     	   		                    if(i==2) supplier.put("Arrive_Time",Machine_Time_N);
   	     	   		                    if(i==3) supplier.put("Arrive_Time",Machine_Time_E);
   	     	   		                         //一下数据  后续 需要用到，    
   	     	   		                    
   	     	   		                    supplier.put("Machine_Address", Machine_Address);
   	     	   		                    supplier.put("Machine_Name", Machine_Name);
   	     	   		                    supplier.put("Supplier_Telnum", Supplier_Telnum);
   	     	   		                    supplier.put("Owner_Telnum", Owner_Telnum);
   	     	   		                    supplier.put("Index_Time_Flag", Time_Flag);
   	     	   		                    supplier.put("Index_Temp_Flag", String.valueOf(Machine_Temp_Config.charAt(i-1)));  
   	     	   		                    
   	     	   						    Suppliers.add(supplier);
 	     	   		                        } // 条件检查， 供应商包含满足条件的目录，并且目录不为空。
   	     	   		                         } // 条件检查，供应商是否授权     	   		                           	   		            		
   	   		            	           }// for  对每一个时间段内，查询所有机器供应商分配中的供应商细分。
   	   		             
   	   		             // 分配 查询失败。
   	   		             }else{
   	   		           result.put("ActionStatus", false);
  				       result.put("ErrorInfo", "供应商查询失败"); 
   	   		             }
   	   		         
   		        	 }
   		         }// for Machine_Time_Flag 
   		      result.put("ActionStatus", true);
			       result.put("ErrorInfo", "查询成功");    
   		      result.put("Suppliers", Suppliers);
   		      /*---------业主 未 授权-----------*/
   		         }else{
   		        	 result.put("ActionStatus", false);
				     result.put("ErrorInfo", "所属业主未授权"); 
   		        	 
   		         }
    		  
    		   
    		   
    		   
    		   
    		   return result;
	    }
}
